import Vue from 'vue'
import VueRouter from 'vue-router'
// import routes from './routes'

var routes = [
  {
    path: '/login',
    name: 'login',
    meta:{title:"登陆"},
    component: () => import('../views/Login.vue')
  },
  {
      path:'/',
      redirect:'/index'
  },
  {
      path: "*",
      component: () => import('../views/NotFound.vue')
  }
]


Vue.use(VueRouter)

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

//设置导航守卫
router.beforeEach((to,from,next)=>{
  //动态修改网页标题
  document.title = to.meta.title;

  //获取token
  var token = localStorage.getItem('token');
  if( to.path == '/login' ){
    if( token ){
      next('/index');
    }else{
      next()
    }
  }
  else{
    if( token ){
      next();
    }else{
      next('/login');
    }
  }
  
})

export default router
